/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'homepagetype',
                condition: {},
                model: {
                    url: _global.url.api + 'homepagetype',
                    event: {
                        '.item-remove:click': 'remove',
                        '.show-offline:click': 'sethidden',
                        '.show-online:click': 'setshow',
                        '.edit_listorder:change': 'setlistorder',
                        '.item-edit': 'edit'
                    }, edit: function (e) {
                        e.preventDefault();
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '修改首页广告',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function () {
                                _this.data.show = $("input[name='show']:checked").val();
                                if (_this.data.show != 0) {
                                    if ($("input[name='ios_type']:checked").val() == 1 && $("input[name='and_type']:checked").val() == 1) {
                                        _this.data.os_type = 3;
                                    } else if ($("input[name='ios_type']:checked").val() == 1 && $("input[name='and_type']:checked").val() == undefined) {
                                        _this.data.os_type = 1;
                                    } else if ($("input[name='and_type']:checked").val() == 1 && $("input[name='ios_type']:checked").val() == undefined) {
                                        _this.data.os_type = 2;
                                    } else if ($("input[name='ios_type']:checked").val() == undefined && $("input[name='and_type']:checked").val() == undefined) {
                                        Alert({msg: '请选择要显示的系统类型', type: 'error'});
                                        return false;
                                    }
                                    _this.data.min_ios_version = $("input[name='min_ios_version']").val();
                                    _this.data.max_ios_version = $("input[name='max_ios_version']").val();
                                    _this.data.min_android_version = $("input[name='min_android_version']").val();
                                    _this.data.max_android_version = $("input[name='max_android_version']").val();
                                }
                                _this.data.intro = $('#intro').val();
                                if (_this.data.intro == '') {
                                    Alert({msg: '请输入布局类型的备注', type: 'error'});
                                    return false;
                                }
                                _this.data.listorder = $('#listorder').val();
                                _this.data.id = _this.data.id;
                                if (model.put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    setshow: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'homepagetype_set',
                                        data: {id: id, is_visible: 1, act: 'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    sethidden: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'homepagetype_set',
                                        data: {id: id, is_visible: 0, act: 'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    setlistorder: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if (new Model({
                                url: _global.url.api + 'homepagetype_set',
                                data: {id: id, listorder: listorder, act: 'order'}
                            }).put()) {
                            _this.collection.fetch();
                        }
                        ;
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $("#btn-add").on("click", function (e) {
                e.preventDefault();
                var html = template("add-tpl");
                dialog({
                    'title': '添加布局类型',
                    'content': html,
                    'width': 500,
                    ok: function () {
                        _this.data.show = $("input[name='show']:checked").val();
                        if (_this.data.show != 0) {
                            if ($("input[name='ios_type']:checked").val() == 1 && $("input[name='and_type']:checked").val() == 1) {
                                _this.data.os_type = 3;
                            } else if ($("input[name='ios_type']:checked").val() == 1 && $("input[name='and_type']:checked").val() == undefined) {
                                _this.data.os_type = 1;
                            } else if ($("input[name='and_type']:checked").val() == 1 && $("input[name='ios_type']:checked").val() == undefined) {
                                _this.data.os_type = 2;
                            } else if ($("input[name='ios_type']:checked").val() == undefined && $("input[name='and_type']:checked").val() == undefined) {
                                Alert({msg: '请选择要显示的系统类型', type: 'error'});
                                return false;
                            }
                            _this.data.min_ios_version = $("input[name='min_ios_version']").val();
                            _this.data.max_ios_version = $("input[name='max_ios_version']").val();
                            _this.data.min_android_version = $("input[name='min_android_version']").val();
                            _this.data.max_android_version = $("input[name='max_android_version']").val();
                        }
                        _this.data.type = $("#type").val();
                        if (_this.data.type == 0) {
                            Alert({msg: '请选择你要添加的布局类型', type: 'error'});
                            return false;
                        }
                        _this.data.intro = $('#intro').val();
                        if (_this.data.intro == '') {
                            Alert({msg: '请输入布局类型的备注', type: 'error'});
                            return false;
                        }
                        _this.data.listorder = $('#listorder').val();
                        if (new Model({
                                url: _global.url.api + 'homepagetype',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                        ;
                    }, cancel: function () {
                    }
                }).show();
            });

            $("#btn-clare").on("click", function (e) {
                e.preventDefault();
                dialog({
                    'title': '清除首页布局缓存',
                    'content': '已经测试了吗？确定清除吗',
                    ok: function () {
                        if (new Model({
                                url: _global.url.api + 'clear_rediskey'
                            }).put()) {
                            _this.collection.reload();
                        }
                        ;
                    }, cancel: function () {
                    }
                }).show();
            });

            $("#btn-test-config").on("click", function (e) {
                e.preventDefault();
                $.ajax({
                    url: _global.url.api + 'homepage_test_config',
                    data: {act: 'get'},
                    type: 'post',
                    async: false,
                    success: function (res) {
                        if (res.code == 0) {
                            dialog({
                                'title': '测试首页预发布设置',
                                width: 800,
                                'content': template("test-config-tpl", res.data),
                                ok: function () {
                                    var data = {};
                                    data.act = 'set';
                                    data.is_open = $('input[name=is_open]:checked').val();
                                    data.user_tag_ids = $('input[name=user_tag_ids]').val();
                                    if (new Model({
                                            url: _global.url.api + 'homepage_test_config',
                                            data: data
                                        }).post()) {
                                        return true;
                                    }
                                    return false;
                                }, cancel: function () {
                                }
                            }).show();
                        }
                    }
                });
            });

        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});